<?php
/**
*	MYSQL数据操作类
* 	@author zengfy 2008.3.4
* 	@modify_record
* 	@example 
* 	$o_dbmysql = new c_dbmysql();
* 	//查询
* 	$result = $o_dbmysql->fetch_query('select id,name from tablename');
* 	结果集形式为 $result = array(
* 							[0] => array('id'=>$value,'name'=>$value),
* 							[1] => array('id'=>$value,'name'=>$value),
* 							...
* 						)
* 	//插入,修改,删除
* 	$o_dbmysql->query($sql);
*/
class common_mysql {

	protected  $querynum;
	protected  $charset;
	static protected $link_identifier;
	protected  $debug_query;
	protected  $sql;
	
	//构造函数,初始化
	function __construct(){
		$this->querynum=0;
		$this->charset='utf8';
		$this->debug_query=null;
		$this->sql = '';
		$this->connect();
	}
	
	//析构函数
	function __destruct(){
		//if($this->link_identifier)
			//$this->close();
	}
	
	//建立数据库连接
	function connect($dbname=DB_NAME, $pconnect=0){
		if(self::$link_identifier){
			return;
		}
		if($pconnect){
			if(!self::$link_identifier=@mysql_pconnect(DB_HOST, DB_USER, DB_PASS)){
				exit('Can not connect to MySQL server');
			}
		}else{
			if(!self::$link_identifier=@mysql_connect(DB_HOST, DB_USER, DB_PASS)){
				exit('Can not connect to MySQL server');
			}
		}

		mysql_query('SET character_set_connection='.$this->charset.', character_set_results='.$this->charset.', character_set_client=binary', self::$link_identifier);
		mysql_query("SET sql_mode=''", self::$link_identifier);

		if($dbname){
			mysql_select_db($dbname, self::$link_identifier);
		}
	}

	function select_db($dbname){
		return mysql_select_db($dbname, self::$link_identifier);
	}

	function fetch_array($query, $result_type=MYSQL_ASSOC){
		@ini_set("memory_limit",-1);
		return mysql_fetch_array($query, $result_type);
	}

	function query($sql, $type=''){
		global $_IGLOBAL;

		$this->get_debug_query();
		//TODO $_IGLOBAL['debug_sqlnum']++;
		$this->sql = $sql;
		$func=($type == 'UNBUFFERED') ? 'mysql_unbuffered_query' : 'mysql_query';
		$query=$func($sql, self::$link_identifier);
		if(!$query){
			exit('MySQL Query Error:'.$func.' SQL:'.$sql.' Error:'.mysql_error(self::$link_identifier));
		}
		$this->get_debug_query('end', $query);
		return $query;
	}

	/**
	 * 调试用
	 */
	function get_debug_query($type='start', $query='') {
		return;
		//TODO
		if(D_BUG){
			global $_IGLOBAL;
			if($type=='start') {
				$_IGLOBAL['debug_sqltime'] = array('start' => 0, 'end' => 0);
				$mtime = explode(' ', microtime());
				$_IGLOBAL['debug_sqltime']['start'] = number_format(($mtime[1] + $mtime[0] - $_IGLOBAL['ifeng_starttime']), 6) * 1000;
			} elseif($type=='end') {
				$mtime = explode(' ', microtime());
				$_IGLOBAL['debug_sqltime']['end'] = number_format(($mtime[1] + $mtime[0] - $_IGLOBAL['ifeng_starttime']), 6) * 1000;
				$sqltime = round(($_IGLOBAL['debug_sqltime']['end'] - $_IGLOBAL['debug_sqltime']['start']), 3);
				$explain = array();
				$info = mysql_info();
				if($query && preg_match("/^(select )/i", $this->sql)) {
					$explain = mysql_fetch_assoc(mysql_query('EXPLAIN '.$this->sql, self::$link_identifier));
				}
				$_IGLOBAL['debug_query'][] = array('sql'=>$this->sql, 'time'=>$sqltime, 'info'=>$info, 'explain'=>$explain);
			}
		} else {
			return 'debug is off';
		}
	}
	
	function get_sql(){
		return $this->sql;
	}

	function affected_rows(){
		return mysql_affected_rows(self::$link_identifier);
	}

	function error(){
		return ((self::$link_identifier) ? mysql_error(self::$link_identifier) : mysql_error());
	}

	function errno(){
		return intval((self::$link_identifier) ? mysql_errno(self::$link_identifier) : mysql_errno());
	}

	function result($query, $row){
		$query=@mysql_result($query, $row);
		return $query;
	}

	function num_rows($query){
		$query=mysql_num_rows($query);
		return $query;
	}

	function num_fields($query){
		return mysql_num_fields($query);
	}

	function free_result($query){
		return mysql_free_result($query);
	}

	function insert_id(){
		return ($id=mysql_insert_id(self::$link_identifier)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}

	function fetch_row($query){
		$query=mysql_fetch_row($query);
		return $query;
	}

	function version(){
		return mysql_get_server_info(self::$link_identifier);
	}

	function close(){
		return is_resource(self::$link_identifier)?mysql_close(self::$link_identifier):true;
	}
	
	// 扩展方法,直接取回query返回的结果集,以数组形式返回
	function fetch_query($sql,$type='',$result_type=MYSQL_ASSOC){
		$result=$this->query($sql,$type);
		$value=array();
		while ($row = $this->fetch_array($result,$result_type)) {
			$value[]=$row;
		}
		return $value;
	}
	//取单行记录
	function fetch_row_query($sql,$type='',$result_type=MYSQL_ASSOC){
		$result=$this->query($sql,$type);
		$row = $this->fetch_array($result,$result_type);
		return $row;
	}
	/**
	 * start transaction
	 *
	 */
	function begin_transaction(){
		 $this->query('START TRANSACTION');	
	}
	/**
	 * rollback
	 *
	 */
	function roll_back(){
		$this->query('ROLLBACK');
	}
	/**
	 * commit
	 *
	 */
	function commit(){
		$this->query('COMMIT');
	}
}
?>
